require 'rexml/document'
include REXML


class ImageCollection
  
  attr_accessor :collection_name, :collection_code, :parent_collection_id, :gender, :real_time, :collection_id
  
  
  def initialize(collection_name, collection_code, parent_collection_id = nil, gender = nil, real_time=false, collection_id = nil)
      
      @collection_name = collection_name
      @collection_code = collection_code
      @parent_collection_id = parent_collection_id
      @gender = gender
      @real_time = real_time
      @collection_id = collection_id

  end

  def toXml()
    
    doc = Document.new()
    doc.add_element("image-collection")
    if (@collection_id != nil)
      node = Element.new("collection-id")
      node.text = @collection_id
      doc.root.elements << node
    end
    node = Element.new("collection-name")
    node.text = @collection_name
    doc.root.elements << node
    node = Element.new("collection-code")
    node.text = @collection_code
    doc.root.elements << node
    if (@parent_collection_id != nil)
      node = Element.new("parent-collection-id")
      node.text = @parent_collection_id
      doc.root.elements << node
    end
    if (@gender != nil)
      node = Element.new("gender")
      node.text = @gender
      doc.root.elements << node
    end
    if (@real_time != nil)
      node = Element.new("real-time")
      node.text = @real_time.to_s()
      doc.root.elements << node
    end    
    
    return doc.to_s()
    
  end
  
  def self.element_value(node, expression)
    
    elements = node.elements[expression]
    
    if (elements != nil) 
      return elements[0].to_s()
    else
      return nil
    end
    
  end
  
  def self.fromXml(doc) 

    name = element_value(doc, "image-collection/collection-name")
    code = element_value(doc, "image-collection/collection-code")

    
    c = ImageCollection.new(name, code)
    c.collection_id = element_value(doc, "image-collection/collection-id") 
    c.parent_collection_id = element_value(doc, "image-collection/parent-collection-id") 
    c.gender = element_value(doc, "image-collection/gender") 
    c.real_time = element_value(doc, "image-collection/real-time")
  
    return c
    
  end

end